package org.dousb.douojbackendjudgeservice.rabbitmq;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * InitRabbitMq
 *
 * @author 豆沙包
 * @version 1.0
 * @since 2025-11-05  23:04
 */
@Slf4j
public class InitRabbitMq {

    /**
     * 主方法，用于创建RabbitMQ连接、通道、交换器和队列，并进行绑定
     */
    public static void doInit() {
        // 创建连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 设置连接主机地址为本地
        connectionFactory.setHost("localhost");
        try {
            // 创建新的连接
            Connection connection = connectionFactory.newConnection();
            // 创建通道
            Channel channel = connection.createChannel();
            // 定义交换器名称
            String EXCHANGE_NAME = "code_exchange";
            // 声明一个类型为direct的交换器
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            //创建一个队列 随机分配一个队列名称
            String queueName = "code_queue";
            channel.queueDeclare(queueName, true, false, false, null);
            channel.queueBind(queueName, EXCHANGE_NAME, "my_routingKey");
            log.info("RabbitMQ初始化成功");
        } catch (Exception e) {
            log.error("RabbitMQ初始化失败");
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        doInit();
    }
}